﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSunny.Admin.Controls
{
    public partial class EditCategory : System.Web.UI.UserControl
    {
        public string catename;
        public string image;
        public string Summary;
        public string description;
        public string showcatetop;
        public string showcateleft;
        public int intactivetop;
        public int intactiveleft;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["catid"] != null)
            {
                int cateid = Convert.ToInt32(Request.QueryString["catid"]);
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID1", cateid.ToString());
                catename = dt.Rows[0]["Category_Name"].ToString();
                image = dt.Rows[0]["Category_Image"].ToString();
                description = dt.Rows[0]["Category_Description"].ToString();
                Summary = dt.Rows[0]["Category_Summary"].ToString();
                showcatetop = dt.Rows[0]["Category_ActiveTop"].ToString();
                showcateleft = dt.Rows[0]["Category_ActiveLeft"].ToString();
                if (showcatetop == "1")
                {
                    showcatetop = "Có";
                }
                else
                {
                    showcatetop = "Không";
                }
                if (showcateleft == "1")
                {
                    showcateleft = "Có";
                }
                else
                {
                    showcateleft = "Không";
                }

                txtCateName.Text = catename;
               CKEditorMota.Text = description;
               CKEditorSummary.Text = Summary;
                ddlActiveTop.Text = showcatetop;
                ddlActiveLeft.Text = showcateleft;
            }
        }

        protected void btnUpdateCate_Click(object sender, EventArgs e)
        {
            int cateid = Convert.ToInt32(Request.QueryString["catid"]);
            string catename = txtCateName.Text;
            string description = CKEditorMota.Text;
            string activetop = ddlActiveTop.Text;
            string activeleft = ddlActiveLeft.Text;
            string filePath = null;
            string image = "";
            if (activetop == "Có")
            {
                intactivetop = 1;
            }
            else
            {
                intactivetop = 0;
            }
            if (activeleft == "Có")
            {
                intactiveleft = 1;
            }
            else
            {
                intactiveleft = 0;
            }
            DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID1", cateid.ToString());
            image = dt.Rows[0]["Category_Image"].ToString();

            if (catename != "")
            {
                if (fulImage.HasFile)
                {
                    if (LIB.Validate.CheckFileType(fulImage.FileName))
                    {
                        string link = fulImage.FileName;
                        LIB.Validate saveimage = new LIB.Validate();
                        saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Image-Category/"), link, 250, fulImage.PostedFile.InputStream);
                        filePath = "/Resources/Images/Image-Category/" + fulImage.FileName;
                        if (image == "")
                        {
                            DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[6];
                            cl[0] = new DAL.DHelpers.Column("Category_Name", catename, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[1] = new DAL.DHelpers.Column("Category_Image", filePath, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[2] = new DAL.DHelpers.Column("Category_Description", description, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[3] = new DAL.DHelpers.Column("Category_ActiveTop", intactivetop.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                            cl[4] = new DAL.DHelpers.Column("Category_ActiveLeft", intactiveleft.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                            cl[5] = new DAL.DHelpers.Column("Category_Summary", CKEditorSummary.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            string colWhere = "Category_ID1";

                            string colValue = cateid.ToString();

                            DAL.DCollector.DBCollector.Instance.StoredUpdate("tbl_Category", cl, colWhere, colValue);
                            Response.Redirect("/Admin/Index.aspx?panel=2&sub=1");
                        }
                        else
                        {
                            File.Delete(MapPath(image));
                            DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[6];
                            cl[0] = new DAL.DHelpers.Column("Category_Name", catename, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[1] = new DAL.DHelpers.Column("Category_Image", filePath, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[2] = new DAL.DHelpers.Column("Category_Description", description, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[3] = new DAL.DHelpers.Column("Category_ActiveTop", intactivetop.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                            cl[4] = new DAL.DHelpers.Column("Category_ActiveLeft", intactiveleft.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                            cl[5] = new DAL.DHelpers.Column("Category_Summary", CKEditorSummary.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            string colWhere = "Category_ID1";

                            string colValue = cateid.ToString();

                            DAL.DCollector.DBCollector.Instance.StoredUpdate("tbl_Category", cl, colWhere, colValue);
                            Response.Redirect("/Admin/Index.aspx?panel=2&sub=1");
                        }

                    }
                    else
                    {
                        Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                    }
                }

                else
                {
                    DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[5];
                    cl[0] = new DAL.DHelpers.Column("Category_Name", catename, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    cl[1] = new DAL.DHelpers.Column("Category_Description", description, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    cl[2] = new DAL.DHelpers.Column("Category_ActiveTop", intactivetop.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                    cl[3] = new DAL.DHelpers.Column("Category_ActiveLeft", intactiveleft.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                    cl[4] = new DAL.DHelpers.Column("Category_Summary", CKEditorSummary.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    string colWhere = "Category_ID1";

                    string colValue = cateid.ToString();

                    DAL.DCollector.DBCollector.Instance.StoredUpdate("tbl_Category", cl, colWhere, colValue);
                    Response.Redirect("/Admin/Index.aspx?panel=2&sub=1");
                }
            }
            else
            {
                Response.Write("<script>alert('Vui lòng nhập tên danh mục...')</script>");
            }
        }
    }
}